/**
 */
package org.mro.grib.model.files.secondEdition.sections.impl;

import java.nio.ByteBuffer;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.mro.grib.core.ProductException;
import org.mro.grib.core.SectionException;
import org.mro.grib.model.files.secondEdition.products.IProductDefinition;
import org.mro.grib.model.files.secondEdition.products.ProductDefinitionTemplateNumber;
import org.mro.grib.model.files.secondEdition.sections.ProductDefinitionSection;
import org.mro.grib.model.files.secondEdition.sections.SectionsPackage;
import org.mro.grib.util.GribSecondEditionTemplatesFactory;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Product Definition Section</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.ProductDefinitionSectionImpl#getStartOffset <em>Start Offset</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.ProductDefinitionSectionImpl#getEndOffset <em>End Offset</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.ProductDefinitionSectionImpl#getLength <em>Length</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.ProductDefinitionSectionImpl#getNumberOfCoordinateValuesAfterTemplate <em>Number Of Coordinate Values After Template</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.ProductDefinitionSectionImpl#getProductDefinitionTemplateNumber <em>Product Definition Template Number</em>}</li>
 *   <li>{@link org.mro.grib.model.files.secondEdition.sections.impl.ProductDefinitionSectionImpl#getProductDefinitionTemplate <em>Product Definition Template</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ProductDefinitionSectionImpl extends EObjectImpl implements ProductDefinitionSection {
	/**
	 * The default value of the '{@link #getStartOffset() <em>Start Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartOffset()
	 * @generated
	 * @ordered
	 */
	protected static final long START_OFFSET_EDEFAULT = 0L;

	/**
	 * The cached value of the '{@link #getStartOffset() <em>Start Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartOffset()
	 * @generated
	 * @ordered
	 */
	protected long startOffset = START_OFFSET_EDEFAULT;

	/**
	 * The default value of the '{@link #getEndOffset() <em>End Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEndOffset()
	 * @generated
	 * @ordered
	 */
	protected static final long END_OFFSET_EDEFAULT = 0L;

	/**
	 * The cached value of the '{@link #getEndOffset() <em>End Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEndOffset()
	 * @generated
	 * @ordered
	 */
	protected long endOffset = END_OFFSET_EDEFAULT;

	/**
	 * The default value of the '{@link #getLength() <em>Length</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLength()
	 * @generated
	 * @ordered
	 */
	protected static final int LENGTH_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getLength() <em>Length</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getLength()
	 * @generated
	 * @ordered
	 */
	protected int length = LENGTH_EDEFAULT;

	/**
	 * The default value of the '{@link #getNumberOfCoordinateValuesAfterTemplate() <em>Number Of Coordinate Values After Template</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNumberOfCoordinateValuesAfterTemplate()
	 * @generated
	 * @ordered
	 */
	protected static final short NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getNumberOfCoordinateValuesAfterTemplate() <em>Number Of Coordinate Values After Template</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNumberOfCoordinateValuesAfterTemplate()
	 * @generated
	 * @ordered
	 */
	protected short numberOfCoordinateValuesAfterTemplate = NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE_EDEFAULT;

	/**
	 * The default value of the '{@link #getProductDefinitionTemplateNumber() <em>Product Definition Template Number</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProductDefinitionTemplateNumber()
	 * @generated
	 * @ordered
	 */
	protected static final short PRODUCT_DEFINITION_TEMPLATE_NUMBER_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getProductDefinitionTemplateNumber() <em>Product Definition Template Number</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProductDefinitionTemplateNumber()
	 * @generated
	 * @ordered
	 */
	protected short productDefinitionTemplateNumber = PRODUCT_DEFINITION_TEMPLATE_NUMBER_EDEFAULT;

	/**
	 * The cached value of the '{@link #getProductDefinitionTemplate() <em>Product Definition Template</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getProductDefinitionTemplate()
	 * @generated
	 * @ordered
	 */
	protected IProductDefinition productDefinitionTemplate;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ProductDefinitionSectionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SectionsPackage.Literals.PRODUCT_DEFINITION_SECTION;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public long getStartOffset() {
		return startOffset;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setStartOffset(long newStartOffset) {
		long oldStartOffset = startOffset;
		startOffset = newStartOffset;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__START_OFFSET, oldStartOffset, startOffset));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public long getEndOffset() {
		return endOffset;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setEndOffset(long newEndOffset) {
		long oldEndOffset = endOffset;
		endOffset = newEndOffset;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__END_OFFSET, oldEndOffset, endOffset));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getLength() {
		return length;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setLength(int newLength) {
		int oldLength = length;
		length = newLength;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__LENGTH, oldLength, length));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public short getNumberOfCoordinateValuesAfterTemplate() {
		return numberOfCoordinateValuesAfterTemplate;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setNumberOfCoordinateValuesAfterTemplate(short newNumberOfCoordinateValuesAfterTemplate) {
		short oldNumberOfCoordinateValuesAfterTemplate = numberOfCoordinateValuesAfterTemplate;
		numberOfCoordinateValuesAfterTemplate = newNumberOfCoordinateValuesAfterTemplate;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE, oldNumberOfCoordinateValuesAfterTemplate, numberOfCoordinateValuesAfterTemplate));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public short getProductDefinitionTemplateNumber() {
		return productDefinitionTemplateNumber;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setProductDefinitionTemplateNumber(short newProductDefinitionTemplateNumber) {
		short oldProductDefinitionTemplateNumber = productDefinitionTemplateNumber;
		productDefinitionTemplateNumber = newProductDefinitionTemplateNumber;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE_NUMBER, oldProductDefinitionTemplateNumber, productDefinitionTemplateNumber));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public IProductDefinition getProductDefinitionTemplate() {
		return productDefinitionTemplate;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetProductDefinitionTemplate(IProductDefinition newProductDefinitionTemplate, NotificationChain msgs) {
		IProductDefinition oldProductDefinitionTemplate = productDefinitionTemplate;
		productDefinitionTemplate = newProductDefinitionTemplate;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE, oldProductDefinitionTemplate, newProductDefinitionTemplate);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setProductDefinitionTemplate(IProductDefinition newProductDefinitionTemplate) {
		if (newProductDefinitionTemplate != productDefinitionTemplate) {
			NotificationChain msgs = null;
			if (productDefinitionTemplate != null)
				msgs = ((InternalEObject)productDefinitionTemplate).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE, null, msgs);
			if (newProductDefinitionTemplate != null)
				msgs = ((InternalEObject)newProductDefinitionTemplate).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE, null, msgs);
			msgs = basicSetProductDefinitionTemplate(newProductDefinitionTemplate, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE, newProductDefinitionTemplate, newProductDefinitionTemplate));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE:
				return basicSetProductDefinitionTemplate(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__START_OFFSET:
				return getStartOffset();
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__END_OFFSET:
				return getEndOffset();
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__LENGTH:
				return getLength();
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE:
				return getNumberOfCoordinateValuesAfterTemplate();
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE_NUMBER:
				return getProductDefinitionTemplateNumber();
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE:
				return getProductDefinitionTemplate();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__START_OFFSET:
				setStartOffset((Long)newValue);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__END_OFFSET:
				setEndOffset((Long)newValue);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__LENGTH:
				setLength((Integer)newValue);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE:
				setNumberOfCoordinateValuesAfterTemplate((Short)newValue);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE_NUMBER:
				setProductDefinitionTemplateNumber((Short)newValue);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE:
				setProductDefinitionTemplate((IProductDefinition)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__START_OFFSET:
				setStartOffset(START_OFFSET_EDEFAULT);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__END_OFFSET:
				setEndOffset(END_OFFSET_EDEFAULT);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__LENGTH:
				setLength(LENGTH_EDEFAULT);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE:
				setNumberOfCoordinateValuesAfterTemplate(NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE_EDEFAULT);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE_NUMBER:
				setProductDefinitionTemplateNumber(PRODUCT_DEFINITION_TEMPLATE_NUMBER_EDEFAULT);
				return;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE:
				setProductDefinitionTemplate((IProductDefinition)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__START_OFFSET:
				return startOffset != START_OFFSET_EDEFAULT;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__END_OFFSET:
				return endOffset != END_OFFSET_EDEFAULT;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__LENGTH:
				return length != LENGTH_EDEFAULT;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE:
				return numberOfCoordinateValuesAfterTemplate != NUMBER_OF_COORDINATE_VALUES_AFTER_TEMPLATE_EDEFAULT;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE_NUMBER:
				return productDefinitionTemplateNumber != PRODUCT_DEFINITION_TEMPLATE_NUMBER_EDEFAULT;
			case SectionsPackage.PRODUCT_DEFINITION_SECTION__PRODUCT_DEFINITION_TEMPLATE:
				return productDefinitionTemplate != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (startOffset: "); //$NON-NLS-1$
		result.append(startOffset);
		result.append(", endOffset: "); //$NON-NLS-1$
		result.append(endOffset);
		result.append(", length: "); //$NON-NLS-1$
		result.append(length);
		result.append(", numberOfCoordinateValuesAfterTemplate: "); //$NON-NLS-1$
		result.append(numberOfCoordinateValuesAfterTemplate);
		result.append(", productDefinitionTemplateNumber: "); //$NON-NLS-1$
		result.append(productDefinitionTemplateNumber);
		result.append(')');
		return result.toString();
	}

	/* (non-Javadoc)
	 * @see org.mro.grib.model.files.secondEdition.sections.ProductDefinitionSection#initialise(java.nio.ByteBuffer)
	 */
	@Override
	public void initialise(ByteBuffer buffer) throws SectionException {
		numberOfCoordinateValuesAfterTemplate = buffer.getShort();
		productDefinitionTemplateNumber = buffer.getShort();
		try {
			productDefinitionTemplate = GribSecondEditionTemplatesFactory.createGribSecondEdition(ProductDefinitionTemplateNumber.get(productDefinitionTemplateNumber));
		} catch (ProductException e) {
			throw new SectionException(e.getMessage());
		}
		productDefinitionTemplate.initialise(buffer);
	}

} //ProductDefinitionSectionImpl
